unit UFrameSearch;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, DBCtrls, Buttons, ExtCtrls, ToolWin, ComCtrls,
  USearchCriteria, DB, Menus;

type
  TFrameSearch = class(TFrame)
    CoolBar: TCoolBar;
    paToolsNavigate: TPanel;
    btExecutar: TSpeedButton;
    btCancelar: TSpeedButton;
    btTools: TSpeedButton;
    btAjuda: TSpeedButton;
    DBNavigator: TDBNavigator;
    paToolsEdit: TPanel;
    dsFrameSearch: TDataSource;
    PopupMenuTools: TPopupMenu;
    mniClearCache: TMenuItem;
    procedure btToolsClick(Sender: TObject);
    procedure mniClearCacheClick(Sender: TObject);
    procedure btExecutarClick(Sender: TObject);
  private
    FListObjects: TList;
    { Private declarations }
    procedure DeleteList;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent) ; override;
    destructor Destroy; override;
    procedure AddSearchCriteria(AFieldName: string; AFieldType: TFieldType; AFieldDisplay: string);
  end;

implementation

{$R *.dfm}

constructor TFrameSearch.Create(AOwner: TComponent);
begin
  inherited;
  FListObjects := TList.Create;
  AddSearchCriteria('PES_NOME', ftString, 'Nome');
  AddSearchCriteria('PES_CODIGO', ftInteger, 'Codigo');
end;

procedure TFrameSearch.AddSearchCriteria(AFieldName: string; AFieldType: TFieldType; AFieldDisplay: string);
begin
  FListObjects.Add(TSearchCriteria.Create(paToolsEdit, AFieldName, AFieldType, AFieldDisplay));
end;

procedure TFrameSearch.DeleteList;
var
  i: integer;
begin
  for i := 0 to FListObjects.Count - 1 do
    TObject(FListObjects.Items[i]).Free;
end;

destructor TFrameSearch.Destroy;
begin
  DeleteList;
  inherited;
end;

procedure TFrameSearch.btToolsClick(Sender: TObject);
var
  APoint: TPoint;
begin
  APoint := btTools.ClientToScreen(Point(0, btTools.ClientHeight));
  PopupMenuTools.Popup(APoint.X, APoint.Y);
end;

procedure TFrameSearch.mniClearCacheClick(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to FListObjects.Count - 1 do
    TSearchCriteria(FListObjects.Items[i]).Clear;
end;

procedure TFrameSearch.btExecutarClick(Sender: TObject);
var
  i: integer;
  s: string;
begin
  s := '';
  for i := 0 to FListObjects.Count - 1 do
      if TSearchCriteria(FListObjects.Items[i]).PropertyToSQL <> '' then
        s := s + TSearchCriteria(FListObjects.Items[i]).PropertyToSQL + ' and ';

  showmessage(s);
end;

end.
